<!-- 审核改派 -->
<template>
  <view class="container">
    <FilterNavbar>
      <template #center>
        <view class="navbar-title"> 审核改派 </view>
      </template>
    </FilterNavbar>
    <view class="viewports">
      <ve-card type="none">
        <ve-cell label="推荐人">
          <span v-if="!record.referenceUserName && !record.referenceUserPhone">-</span>
          <span v-else>{{ record.referenceUserName || '-' }} {{ record.referenceUserPhone || '-' }}</span>
        </ve-cell>
        <ve-cell label="改派原因">{{ record.reassignmentReason || '-' }}</ve-cell>
      </ve-card>
      <ve-card type="none" fold="true">
        <ve-form ref="ve_form" :model="form" :rules="rules1" v-if="form.examineStatus == 1">
          <ve-form-item label="审核结果" class="car-origin">
            <ve-radio v-model="form.examineStatus" :list="vEnumList" @confirm="radioChange" />
          </ve-form-item>
          <ve-form-item :label="labelName" prop="staffId" :borderBottom="false" required>
            <remote-select
              type="reassignment_staff_list_multiple"
              :params="{
                siteIdList: [record.siteId],
              }"
              v-model="form.staffId"
              v-model:label="form.realname"
            />
          </ve-form-item>
        </ve-form>
        <ve-form ref="ve_form" :model="form" :rules="rules2" v-if="form.examineStatus == 0">
          <ve-form-item label="审核结果" class="car-origin">
            <ve-radio v-model="form.examineStatus" :list="vEnumList" @confirm="radioChange" />
          </ve-form-item>
          <ve-form-item
            label="拒绝原因"
            labelPosition="top"
            :borderBottom="false"
            prop="refuseReassignmentReason"
            required
            v-if="form.examineStatus == 0"
          >
            <ve-input type="textarea" v-model="form.refuseReassignmentReason" placeholder="请输入" :maxlength="200" />
          </ve-form-item>
        </ve-form>
      </ve-card>
    </view>
    <ve-bottom-box>
      <ve-button type="primary" @click="formSubmit" size="long" :loading="okLoading">确认</ve-button>
    </ve-bottom-box>
  </view>
</template>

<script setup lang="ts">
  import { onLoad } from '@dcloudio/uni-app'
  import { ref, nextTick } from 'vue'
  import FilterNavbar from '@/components/FilterNavbar'
  import { examineReassignmentAPI } from '@/api/order/workOrder'
  import useFormFold from '@/hooks/useFormFold'
  const initFold1 = ref(true)
  const record = ref(null)
  const labelName = ref('')
  const vEnumList = ref([
    { value: 1, label: '同意' },
    { value: 0, label: '拒绝' },
  ])
  const form = ref({
    id: '',
    examineStatus: 1,
  })
  const { foldStatus, foldValidate } = useFormFold([
    {
      key: 'fold1',
      props: ['staffId', 'refuseReassignmentReason'],
    },
  ])
  const radioChange = (e) => {
    form.value.staffId = undefined
    form.value.refuseReassignmentReason = undefined
  }
  const rules1 = ref({
    staffId: {
      required: true,
      message: '请选择',
    },
  })
  const rules2 = ref({
    refuseReassignmentReason: {
      required: true,
      message: '请填写拒绝原因',
    },
  })

  const toast = (e) => {
    uni.showToast({
      title: e,
      icon: 'none',
    })
  }
  onLoad((options) => {
    console.log(JSON.parse(options.record))
    record.value = JSON.parse(options.record)
    form.value.id = record.value.id
    form.value.staffId = record.value.referenceUserId
    form.value.realname = record.value.referenceUserName
    if (record.value.workType == 1) {
      labelName.value = '改派的交车人'
      rules1.value = {
        staffId: {
          required: true,
          message: '请选择交车人',
        },
      }
    } else if (record.value.workType == 2) {
      labelName.value = '改派的还车人'
      rules1.value = {
        staffId: {
          required: true,
          message: '请选择还车人',
        },
      }
    } else {
      labelName.value = '改派的换车人'
      rules1.value = {
        staffId: {
          required: true,
          message: '请选择换车人',
        },
      }
    }
  })
  const ve_form = ref(null)
  const okLoading = ref(false)
  const formSubmit = () => {
    ve_form.value.validate((valid, errors) => {
      console.log(valid)
      if (valid) {
        var res
        var params = {
          ...form.value,
        }
        okLoading.value = true
        examineReassignmentAPI(params)
          .then((res) => {
            okLoading.value = false
            if (res.code == 200) {
              toast(res.message)
              uni.$emit('refreshData')
              uni.navigateBack()
            } else {
              toast(res.message)
              return false
            }
          })
          .catch(() => {
            okLoading.value = false
          })
      } else {
        foldValidate(errors)
        console.log('验证失败', errors)
      }
    })
  }
  defineExpose({})
</script>

<style lang="less" scoped>
  .container {
    background: #f7f8fa;
  }

  .viewports {
    display: flex;
    flex-direction: column;
    padding: 24rpx;
  }

  .platoon-model {
    width: 100%;
    background: #f7f8fa;
    padding: 34rpx 24rpx;
    font-size: 30rpx;
    color: #191c27;
  }

  .car-origin {
    /deep/ .ve-form-item__body {
      align-items: center;
    }
  }
</style>
